package com.eelpo.framework.socket;

import java.io.IOException;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBufferOutputStream;
import org.jboss.netty.buffer.ChannelBuffers;

import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Output;

/**
 * 响应类
 * 
 * @author <a href="mailto:whz-work@163.com">汪洪州</a>
 * 
 * @since 1.0 Create on May 30, 2012
 */
public class SocketResponse {

	/**
	 * id
	 */
	private int i;

	/**
	 * status
	 */
	private byte s;

	/**
	 * action
	 */
	private int a;

	/**
	 * value
	 */
	private Object o;

	private static final byte SUCCESS = 0;
	private static final byte FAILURE = 1;
	
	public static SerializationContext serializationContext = new SerializationContext();

	private SocketResponse(byte s, Object o) {
		this.s = s;
		this.o = o;
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

	public byte getS() {
		return s;
	}

	public void setS(byte s) {
		this.s = s;
	}

	public int getA() {
		return a;
	}

	public void setA(int a) {
		this.a = a;
	}

	public Object getO() {
		return o;
	}

	public void setO(Object o) {
		this.o = o;
	}

	public static SocketResponse success(Object o) {
		return new SocketResponse(SUCCESS, o);
	}

	public static SocketResponse failure(Object o) {
		return new SocketResponse(FAILURE, o);
	}

	public ChannelBuffer serialize() throws IOException {
		ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
		ChannelBufferOutputStream outStream = new ChannelBufferOutputStream(channelBuffer);
		Amf3Output amf3Output = new Amf3Output(serializationContext);
		amf3Output.setOutputStream(outStream);
		amf3Output.writeObject(this);
		return channelBuffer;
	}

}
